﻿/****************************************************************************
Copyright (c) 2014-2015 凌惊雪 微信:Lingjingxue 邮箱:lingjingxue@sina.com QQ:271487457

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/

using ServerBase.Protocol;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
using UtilLib;
using UtilPublic.ServerLoger;
using ServerPublic.Data;
using static UtilPublic.Redis.ServerRedis;
using static ServerBase.DataBaseRedis.RdsTxt;

namespace ServerPublic.PubObject
{
    [Desc("账号")]
    public class User
    {
        [Desc("账号Id")]
        public long Uuid = 0;
        [Desc("平台ID")]
        public EPlatform Platform = 0;
        [Desc("账号")]
        public string Account = "account0";
        [Desc("密码")]
        public string Password = "123456";
        [Desc("注册时间")]
        public DateTime TimeRegister = DateTime.Now;
        [Desc("上次登录时间")]
        public DateTime TimeLogin = DateTime.Now;

        [Desc("用户人民币充值总额")]
        public long RechargeRmb = 0;

        //存储
        public bool Save()
        {
            try
            {
                loger.Debug($"User.Save，账号：{Account}");

                var datas = new Dictionary<string, object>()
                {
                    [nameof(Uuid)] = Uuid,
                    [nameof(Account)] = Account,
                    [nameof(Password)] = Password,
                    [nameof(TimeRegister)] = TimeRegister,
                    [nameof(TimeLogin)] = TimeLogin,
                    [nameof(RechargeRmb)] = RechargeRmb,
                };

                AHashSetDict($"{RdsUserData}:{Account}", datas);
                AHashSet(RdsUserUuidAccount, Uuid, Account);

                return true;
            }
            catch (Exception ex)
            {
                loger.Fatal("User.Save 失败", ex);
                return false;
            }
        }
        // 读取
        public bool Load()
        {
            loger.Debug($"User.Load，账号：{Account}");
            try
            {
                var HashId = $"{RdsUserData}:{Account}";
                var datas = AHashGetDict(HashId);

                if (!datas.ContainsKey(nameof(Account)))
                {
                    return false;
                }

                datas.LoadData(nameof(Uuid), ref Uuid);
                datas.LoadData(nameof(Account), ref Account);
                datas.LoadData(nameof(Password), ref Password);
                datas.LoadData(nameof(TimeRegister), ref TimeRegister);
                datas.LoadData(nameof(TimeLogin), ref TimeLogin);
                datas.LoadData(nameof(RechargeRmb), ref RechargeRmb);

                return true;
            }
            catch (Exception ex)
            {
                loger.Debug($"User.Load 失败", ex);
                return false;
            }
        }
    }
}
